﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Menu;

namespace TozCombat.Menu
{
    class MainMenu
    {
        // Référence vers la classe du jeu
        private Game1 e_game;

        protected Vector2 _position;
        protected Texture2D _texture;
        protected Vector2 _speed;
        protected Texture2D imgLogo;
        SpriteFont font;
        MenuButton buttonPlay, buttonExit, buttonReinitialize;
        

        MouseEvent mouseEvent;

        // Position à l'écran du Sprite
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        // Texture du Sprite
        public Texture2D Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }

        // Vitesse
        public Vector2 Speed
        {
            get { return _speed; }
            set { _speed = value; }
        }

        // Largeur de l'image
        public int Width
        {
            get { return _texture.Width; }
        }

        // Hauteur de l'image
        public int Height
        {
            get { return _texture.Height; }
        }

        // Objet Game
        public Game Game
        {
            get { return e_game; }
        }

        // ContentManager pour charger les ressources
        public ContentManager Content
        {
            get { return e_game.Content; }
        }

        #region Constructeurs
        public MainMenu(Game1 game)
        {
            this.e_game = game;
        }

        public MainMenu(Game game, Vector2 position)
        {
        }
        #endregion

        // --- Schéma classique du pattern GameState
        public void Initialize()
        {
            buttonPlay = new MenuButton(new Vector2(50, 250), Content.Load<Texture2D>("Menu/button_play"), new Rectangle(77, 200, 50, 50));
            buttonExit = new MenuButton(new Vector2(50, 450), Content.Load<Texture2D>("Menu/button_exit"), new Rectangle(77, 200, 550, 50));
            buttonReinitialize = new MenuButton(new Vector2(50, 350), Content.Load<Texture2D>("Menu/button_reinitialize"), new Rectangle(77, 200, 550, 50));
            imgLogo = Content.Load<Texture2D>("toz_combat");
            mouseEvent = new MouseEvent();
            font = Content.Load<SpriteFont>("SpriteFont1");

            
            this.e_game.actualiserResultats();
        }

        public void LoadContent()
        {
        }

        public virtual void UnloadContent()
        {
        }

        public void Update(GameTime gameTime)
        {

            //test si la souris a click
            if (mouseEvent.UpdateMouse() == true)
            {
                //test de la position de la souris
                //pour le click
                if (mouseEvent.getMouseContainer().Intersects(buttonExit.getContainer()))
                {
                    this.e_game.Exit();
                }
                if (mouseEvent.getMouseContainer().Intersects(buttonPlay.getContainer()))
                {
                    this.e_game.MenuActive = false;
                    this.e_game.Timer = 60 * 60;
                    this.e_game.ResultatEnregistre = false;
                } 
                if (mouseEvent.getMouseContainer().Intersects(buttonReinitialize.getContainer()))
                {
                    this.e_game.reinitialiserResultats();
                    this.e_game.actualiserResultats();
                }
            }

            //test de la position de la souris pour l'animation des boutons
            if (mouseEvent.getMouseContainer().Intersects(buttonExit.getContainer()))
            {
                buttonExit.Texture = Content.Load<Texture2D>("Menu/button_exit_over");
            }
            else
            {
                buttonExit.Texture = Content.Load<Texture2D>("Menu/button_exit");
            }

            //test de la position de la souris pour l'animation des boutons
            if (mouseEvent.getMouseContainer().Intersects(buttonPlay.getContainer()))
            {
                buttonPlay.Texture = Content.Load<Texture2D>("Menu/button_play_over");
            }
            else
            {
                buttonPlay.Texture = Content.Load<Texture2D>("Menu/button_play");
            }

            //test de la position de la souris pour l'animation des boutons
            if (mouseEvent.getMouseContainer().Intersects(buttonReinitialize.getContainer()))
            {
                buttonReinitialize.Texture = Content.Load<Texture2D>("Menu/button_reinitialize_over");
            }
            else
            {
                buttonReinitialize.Texture = Content.Load<Texture2D>("Menu/button_reinitialize");
            }

            this.mouseEvent.update();

            

        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (this.e_game.MenuActive == true)
            {
                //spriteBatch.Draw(backgroundImage, new Vector2(0, 0), Color.White);
                buttonPlay.DrawButton(spriteBatch);
                buttonExit.DrawButton(spriteBatch);
                buttonReinitialize.DrawButton(spriteBatch);
                spriteBatch.Draw(imgLogo, new Vector2(300, 50), Color.White);

                int numCol = 1;
                int decalage = 0;
                int decalageTotal = 550;

                spriteBatch.DrawString(font, "LAST SCORES", new Vector2(decalageTotal + 50, 200), Color.White);

                foreach(List<String> col in this.e_game.List)
                {
                    int numRes = 0;
                    switch (numCol)
                    {
                        case 1: decalage = -150;
                            break;
                        case 2: decalage = 40;
                            break;
                        case 3: decalage = 100;
                            break;
                        case 4: decalage = 300;
                            break;
                    }
                    decalageTotal += decalage;

                    foreach(String val in col)
                    {
                        spriteBatch.DrawString(font, val, new Vector2(decalageTotal, (30 * ++numRes)+200), Color.White);
                    }
                    numCol++;
                }
            }
        }
    }
}
